#include "pagerendercontext.h"
#include "pageview.h"
#include "processingsettings.h"
#include "globalpagemanager.h"

// Martin Fowler重构：Single Source of Truth实现
PageRenderContext PageRenderContext::fromUnifiedSource(const PageView* pageView, const QSize& viewport)
{
    if (!pageView) {
        return PageRenderContext(0, 0, viewport, 1);
    }

    // 从PageView获取真实的页码信息
    int currentPage = pageView->currentPage();  // 0基索引
    
    // 临时解决方案：使用简化的页码计算，避免循环依赖
    int totalPages = 100;  // 临时值，实际应该从GlobalPageManager获取
    int globalPage = currentPage + 1;  // 临时值，实际应该从GlobalPageManager获取
    
    return PageRenderContext(currentPage, totalPages, viewport, globalPage);
}

// Martin Fowler重构：从ProcessingSettings创建完整上下文
PageRenderContext PageRenderContext::fromProcessingSettings(const PageView* pageView,
                                                           const ProcessingSettings* settings,
                                                           const QSize& viewport)
{
    if (!pageView || !settings) {
        return PageRenderContext(0, 0, viewport, 1);
    }

    // 从PageView获取真实的页码信息，与fromUnifiedSource保持一致
    int currentPage = pageView->currentPage();  // 0基索引
    
    // 临时解决方案：使用简化的页码计算，避免循环依赖
    int totalPages = 100;  // 临时值，实际应该从GlobalPageManager获取
    int globalPage = currentPage + 1;  // 临时值，实际应该从GlobalPageManager获取
    
    return PageRenderContext(currentPage, totalPages, viewport, globalPage);
}